<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>聊天室 - 注册</title>
    <link href="https://fonts.googleapis.com/css2?family=Nunito:wght@400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="/css/style.css">
</head>
<body>
    <div class="auth-container">
        <div class="auth-card animated">
            <div class="auth-header">
                <h2>创建账号</h2>
            </div>
            <div class="auth-body">
                <div id="error-message" class="error-message" style="color: red; margin-bottom: 15px; display: none;"></div>
                <form id="register-form">
                    <div class="form-group">
                        <input type="text" id="username" name="username" class="form-control" placeholder="用户名" required>
                    </div>
                    <div class="form-group">
                        <input type="password" id="password" name="password" class="form-control" placeholder="密码" required>
                    </div>
                    <div class="form-group">
                        <input type="password" id="confirm-password" name="confirm-password" class="form-control" placeholder="确认密码" required>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn" style="width: 100%;">注册</button>
                    </div>
                </form>
            </div>
            <div class="auth-footer">
                <p>已有账号？ <a href="/login" class="auth-switch">立即登录</a></p>
            </div>
        </div>
    </div>

    <script>
        document.getElementById('register-form').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirm-password').value;
            const errorMessage = document.getElementById('error-message');
            
            if (password !== confirmPassword) {
                errorMessage.textContent = '两次输入的密码不一致';
                errorMessage.style.display = 'block';
                return;
            }
            
            fetch('/api/users/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ username, password })
            })
            .then(response => {
                if (response.ok) {
                    return response.json();
                } else {
                    return response.json().then(data => {
                        throw new Error(data.error || '注册失败');
                    });
                }
            })
            .then(data => {
                // 保存用户信息到本地存储
                localStorage.setItem('user', JSON.stringify(data));
                // 跳转到用户列表页面
                window.location.href = '/users';
            })
            .catch(error => {
                errorMessage.textContent = error.message;
                errorMessage.style.display = 'block';
            });
        });
    </script>
</body>
</html> 